package libraryPSI2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;

    public class ConnectToLibrary {

        List<String> toReturn = new LinkedList<String>();

        List<String> allBooks (){
            try
            {
                Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                try
                {
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "root");
                    ps = conn.prepareStatement("Select * from library.books");
                    rs = ps.executeQuery();
                    String tmp;
                    while (rs.next())
                    {
                        tmp =(rs.getInt("idbooks")+ "|"+rs.getString("name")+"|"+ rs.getString("author"));
                        toReturn.add(tmp);
                    }
                }
                finally
                {
                    if (conn != null)
                    {
                        conn.close();
                        conn = null;
                    }
                    if (ps != null)
                    {
                        ps.close();
                        ps = null;
                    }
                    if (rs != null)
                    {
                        rs.close();
                        rs = null;
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return toReturn;        }

        public void showBook(){
            System.out.println(toReturn);
        }

        public boolean addBookToDB(String name,String author)
        {
            boolean f = false;
            try
            {
                Connection conn = null;
                PreparedStatement ps = null;
                try
                {
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "root");
                    ps = conn.prepareStatement("Insert into library.books VALUES(?,?)");

                    ps.setString(2, name);
                    ps.setString(3, author);

                    int res = ps.executeUpdate();
                    f = res > 0;
                }
                finally
                {
                    if (conn != null)
                    {
                        conn.close();
                        conn = null;
                    }
                    if (ps != null)
                    {
                        ps.close();
                        ps = null;
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return f;
        }
    }
